// File Name: flash_task.c
// Author: kukukukuku~
// Date: 2024-07-12

#include "./User/Flash/inc/flash_task.h"
#include "./User/Key/inc/key_ctrl.h"
#include "./User/LED/inc/led_ctrl.h"
#include "./User/OLED/inc/oled_ctrl.h"
#include "./User/Step_Motor/inc/step_motor_ctrl.h"
#include "./User/WS2812B/inc/ws2812b_ctrl.h"

TaskHandle_t taskHandleFlash;   //Flash任务句柄

SemaphoreHandle_t semaphBinFlash;   //Flash二值信号量

/**
 * @brief   Flash任务
 * @param   arg:    任务参数
 * @retval  None
 * @note    None
*/
void task_flash(void *arg)
{
    while(1)
    {
        if(xSemaphoreTake(semaphBinFlash, portMAX_DELAY) == pdTRUE)
        {
            flashData[FLASH_DATA_KEY_CLICK_BEEP_INDEX]   = keyClickBeep;
            flashData[FLASH_DATA_KEY_LONG_BEEP_INDEX]    = keyLongBeep;
            flashData[FLASH_DATA_LED1_MODE_INDEX]        = led1.mode;
            flashData[FLASH_DATA_LED1_BLINK_FREQ_INDEX]  = led1.blinkNum;
            flashData[FLASH_DATA_LED1_BREATH_STEP_INDEX] = led1.breathStep;
            flashData[FLASH_DATA_LED2_MODE_INDEX]        = led2.mode;
            flashData[FLASH_DATA_LED2_BLINK_FREQ_INDEX]  = led2.blinkNum;
            flashData[FLASH_DATA_LED2_BREATH_STEP_INDEX] = led2.breathStep;
            flashData[FLASH_DATA_OLED_DIR_INDEX]         = oledDir;
            flashData[FLASH_DATA_OLED_LIGHT_INDEX]       = oledLightLevel;
            flashData[FLASH_DATA_STEP_MOTOR_MODE_INDEX]  = stepMotorMode;
            flashData[FLASH_DATA_STEP_MOTOR_SPEED_INDEX] = stepMotorSpd;
            flashData[FLASH_DATA_WS2812B_CW_MODE_INDEX]  = ws2812bCwMode;
            flashData[FLASH_DATA_WS2812B_CW_R_INDEX]     = ws2812bCwRed;
            flashData[FLASH_DATA_WS2812B_CW_G_INDEX]     = ws2812bCwGreen;
            flashData[FLASH_DATA_WS2812B_CW_B_INDEX]     = ws2812bCwBlue;
            flashData[FLASH_DATA_WS2812B_CCW_MODE_INDEX] = ws2812bCcwMode;
            flashData[FLASH_DATA_WS2812B_CCW_R_INDEX]    = ws2812bCcwRed;
            flashData[FLASH_DATA_WS2812B_CCW_G_INDEX]    = ws2812bCcwGreen;
            flashData[FLASH_DATA_WS2812B_CCW_B_INDEX]    = ws2812bCcwBlue;
            
            flash_data_save(FLASH_ADDR_DATA_START, flashData, FLASH_DATA_NUM) == 0 ?\
            (flashSaveFlag = 1) : (flashSaveFlag = 2);
        }
    }
}
